package com.flyan.game.motafx.effect;

import cn.hutool.json.JSONObject;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.entity.Entity;
import javafx.util.Duration;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

/**
 * 拓展效果，所有效果基于此类
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/7
 */
@Setter
@Getter
public abstract class BaseEffect extends Effect {

    /**
     * 触发该效果的实体
     */
    protected Entity triggerEntity;

    /**
     * 拓展参数
     */
    protected JSONObject extInfo = new JSONObject();

    /**
     * 是否可重复添加，默认是
     */
    public boolean allowRepeat = true;

    public BaseEffect(@NotNull Duration duration) {
        super(duration);
    }

    public BaseEffect() {
        this(Duration.INDEFINITE);
    }

    public void setExtInfo(String key, Object value) {
        extInfo.set(key, value);
    }

    /**
     * 应用效果
     *
     * @param entity 应用到的实体
     */
    public void apply(Entity entity) {
        if (entity.hasComponent(EffectComponent.class)) {
            EffectComponent effectComponent = entity.getComponent(EffectComponent.class);
            if (allowRepeat) {
                effectComponent.startEffect(this);
            } else {
                if (!effectComponent.hasEffect(this.getClass())) {
                    effectComponent.startEffect(this);
                }
            }
        }
    }

}
